// component/rightList/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    loading: false,
    showDeleteModal: false,
    state: '',
    id: '',
    list: []
  },
  created: function(){
    this.getInviteList()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getInviteList(){
      this.setData({
        loading: true
      })
      wx.cloud.callFunction({
        name: 'getRightList',
        data: {
          state: 'applyRight',
          page: 1
        }
      }).then(res => {
        // console.log(res.result)
        this.setData({
          list: res.result,
          loading: false
        })
      })
    },
    agreeApply(){
      this.setData({
        showDeleteModal: true,
        state: 'agree'
      })
    },
    refuseApply(){
      this.setData({
        showDeleteModal: true,
        state: 'refuse'
      })
    },
    comfireOperate(){
      wx.showLoading({
        title: '操作中',
        mask: true,
      })
      wx.cloud.callFunction({
        name: 'handleRightApply',
        data: {
          id: this.data.id,
          state: this.data.state
        }
      }).then(res => {
        this.setData({
          showDeleteModal: false
        })
        if (res.result == 2) {
          this.data.list = this.data.list.filter(item => {
            return item._id != this.data.id
          })
          this.setData({
            id: '',
            list: this.data.list,
            state: ''
          })
          this.triggerEvent('refreshRightList', true)
          wx.showToast({
            title: '已同意',
            icon: 'success',
            duration: 1000
          })
        }else if(res.result == 3){
          this.data.list = this.data.list.filter(item => {
            return item._id != this.data.id
          })
          this.setData({
            id: '',
            list: this.data.list,
            state: ''
          })
          wx.showToast({
            title: '已拒绝',
            icon: 'success',
            duration: 1000
          })
        }
      })
    },
    hideModal() {
      this.setData({
        showDeleteModal: false,
        state: ''
      })
    },
    // ListTouch触摸开始
    ListTouchStart(e) {
      this.setData({
        id: e.currentTarget.dataset.id,
        ListTouchStart: e.touches[0].pageX
      })
    },

    // ListTouch计算方向
    ListTouchMove(e) {
      this.setData({
        ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
      })
    },

    // ListTouch计算滚动
    ListTouchEnd(e) {
      if (this.data.ListTouchDirection == 'left') {
        this.setData({
          modalName: e.currentTarget.dataset.target
        })
      } else {
        this.setData({
          modalName: null
        })
      }
      this.setData({
        ListTouchDirection: null
      })
    },
  }
})
